// 2. 封装异步action

import {
  getuserporfileAPI,
  updatePhtotAPI,
  updateProfileAPI,
  userinfoAPI,
} from '@/api/user';
import { AppDispatch } from '..';
import { ProfileType } from '../reducer/user';
/** 获取用户信息Action */
export const userinfoAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await userinfoAPI();
    console.log('res  ----->  ', res);
    dispatch({ type: 'user/saveUser', payload: res.data });
  };
};
// 获取个人信息
export const userprofileAction = () => {
  return async (dispatch: AppDispatch) => {
    const res = await getuserporfileAPI();
    console.log(res);
    dispatch({ type: 'user/saveProfile', payload: res.data });
  };
};
// 更新照片
export const UpdatephtotAction = (data: FormData) => {
  return async (dispatch: AppDispatch) => {
    const res = await updatePhtotAPI(data);
    console.log(res.data);
    // 3. 触发保存的action
    dispatch({ type: 'user/updateProfile', payload: res.data });
  };
};

export const UpdateprofileAction = (data: Partial<ProfileType>) => {
  return async (dispatch: AppDispatch) => {
    await updateProfileAPI(data);
    // 复用更新照片的reducer
    dispatch({ type: 'user/updateProfile', payload: data });
  };
};
